<?php

namespace app\admin\controller;
use think\Controller;
use think\image\Exception;

class Positioncontent extends Common {
    public function index (){
        $positions = model("position")->getNormalPositions();
        //获取推荐位内容
        $data['status'] = array('neq', -1);
        if(isset($_GET['title']) && $_GET['title']) {
            $data['title'] = trim($_GET['title']);
        }

        $data['position_id'] = isset($_GET['position_id']) ? intval($_GET['position_id']) : $positions[0]['id'];

        $contents = model('positionContent')->selectData($data);
        $this->assign('postions', $positions);
        $this->assign('contents', $contents);
        $this->assign('positionId', $data['position_id']);
        $this->assign('title', isset($data['title']) ? $data['title'] : '');
        return $this->fetch();
    }

    public function add(){
        if($_POST) {
            if(!isset($_POST['position_id']) || !$_POST['position_id']) {
                return show(0, '推荐位ID不能为空');
            }
            if(!isset($_POST['title']) || !$_POST['title']) {
                return show(0, '推荐位标题不能为空');
            }
            if(!$_POST['url'] && !$_POST['news_id']) {
                return show(0, 'url和news_id不能同时为空');
            }
            if(!isset($_POST['thumb']) || !$_POST['thumb']) {
                if($_POST['news_id']) {
                    $res = model("news")->find($_POST['news_id']);
                    if($res) {
                        $_POST['thumb'] = $res['thumb'];
                    }
                } else {
                    return show(0, '图片不能为空');
                }
            }

            if($_POST['id']) {
                return $this->saveData($_POST);
            }

            try {
                $_POST['create_time'] = time();
                $id = model('positionContent')->insertData($_POST);
                if($id) {
                    return show(1, '新增成功');
                }
                return show(0, '新增失败');
            }catch (Exception $e) {
                return show(0, $e->getMessage());
            }

        } else {
            $positions = model("position")->getNormalPositions();
            $this->assign('positions', $positions);
            return $this->fetch();
        }
    }

    public function edit(){
        $id = $_GET['id'];

        // -----推荐位内容获取-----
        $position = model('positionContent')->findData($id);
        // -----推荐位获取-----
        $positions = model("position")->getNormalPositions();

        $this->assign('positions', $positions);
        $this->assign('vo', $position);
        return $this->fetch();
    }

    public function saveData($data){
        $id = $data['id'];
        unset($data['id']);
        try {
            $resId = model('positionContent')->updateById($id, $data);
            if($resId === false) {
                return show(0, "更新失败");
            }
            return show(1, "更新成功");
        }catch (Exception $e) {
            return show(0,$e->getMessage());
        }
    }

    public function setStatusC(){
        $data = array(
            'id' => intval($_POST['id']),
            'status' => intval($_POST['status'])
        );
        return parent::setStatus($data, 'positionContent');
    }
}